package com.my.study.common.security.remember;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken;

import java.io.Serializable;
import java.util.Date;

/**
 * 兼容Spring Security的{@link PersistentRememberMeToken}，支持序列化
 * 
 * @author yidujun
 * @date 2022/8/5 15:35
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CustomPersistentRememberMeToken implements Serializable {
    private static final long serialVersionUID = 50903965263384250L;

    private String username;
    private String series;
    private String tokenValue;
    private Date date;
    
    public CustomPersistentRememberMeToken(PersistentRememberMeToken persistentRememberMeToken) {
        this.username = persistentRememberMeToken.getUsername();
        this.series = persistentRememberMeToken.getSeries();
        this.tokenValue = persistentRememberMeToken.getTokenValue();
        this.date = persistentRememberMeToken.getDate();
    }
    
    
    public PersistentRememberMeToken toToken(CustomPersistentRememberMeToken customPersistentRememberMeToken) {
        if (null == customPersistentRememberMeToken) {
            return null;
        }
        return new PersistentRememberMeToken(
                customPersistentRememberMeToken.getUsername(),
                customPersistentRememberMeToken.getSeries(),
                customPersistentRememberMeToken.getTokenValue(),
                customPersistentRememberMeToken.getDate()
        );
    }
}
